package com.course.business.controller.admin;

import com.course.server.domain.Course;
import com.course.server.dto.CourseDTO;
import com.course.server.dto.PageDTO;
import com.course.server.dto.ResponseDTO;
import com.course.server.service.CourseService;
import com.course.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/course")
public class CourseController {

    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;

    @PostMapping("/course")
    public String course(){
        return "success";
    }

    @PostMapping("/list")
    public ResponseDTO list(@RequestBody PageDTO pageDTO){
        logger.info("pageDTO:{}", pageDTO);
        ResponseDTO responseDTO = new ResponseDTO();
        courseService.list(pageDTO);
        responseDTO.setContent(pageDTO);
        return responseDTO;
    }

    @PostMapping("/save")
    public ResponseDTO save(@RequestBody CourseDTO courseDTO){
        logger.info("courseDTO:{}", courseDTO);

        //保存校验
        ValidatorUtil.require(courseDTO.getName(), "名称");
        ValidatorUtil.length(courseDTO.getName(), "名称", 1, 50);
        ValidatorUtil.length(courseDTO.getSummary(), "概述", 1, 2000);
        ValidatorUtil.length(courseDTO.getImage(), "封面", 1, 100);


        ResponseDTO responseDTO = new ResponseDTO<>();
        courseService.save(courseDTO);
        responseDTO.setContent(courseDTO);
        return responseDTO;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDTO save(@PathVariable String id){
        logger.info("id:{}", id);
        ResponseDTO responseDTO = new ResponseDTO<>();
        courseService.delete(id);
        return responseDTO;
    }
}
